<?php $this->widget('application.widgets.jui.initial');?>
<?php
            $this->widget('application.widgets.listicon.ListIcon', array(
            		'id'=>'packing_list_update',
                    'icons'=>array(
                        array('value'=>'Add new packages','icon'=>'ui-icon-plus','needtext'=>"true"),
                        array('value'=>'View report','icon'=>'ui-icon-document','needtext'=>"true",'htmlOptions'=>array('href'=>CHtml::normalizeUrl(array('packing_list/report','id'=>$model->id)))),
                    ),
                    'htmlOptions'=>array('style'=>'text-align: right;')
            ));
?>
<div class="view">
<fieldset id="packing_list" class="ui-widget section">
	<legend class="ui-widget-header ui-corner-all">Shipment information</legend>
	<div class="row">
    	<?php echo CHtml::activeLabelEx($model,'packing_details_id')?>
    	<?php echo CHtml::activeDropDownList($model,'packing_details_id',packing_list_details::model()->listDetails())?>
    	<?php echo CHtml::link("Add new",array('packing_list/newdetails')); ?>
    	<?php echo CHtml::link("View",'#'); ?>
    </div>
    <div class='row'>
    	<?php echo CHtml::activeLabelEx($model,'date'); ?>
        <?php $this->widget('zii.widgets.jui.CJuiDatePicker',
                array(
                'model'=>$model,
                'id'=>'update_packing_list',
                'attribute'=>"date",
                'htmlOptions'=>array('size'=>60,'value'=>(!empty($model->date))?(is_numeric($model->date))?date('d/m/Y',$model->date):$model->date:date('d/m/Y')),
                'options'=>array('dateFormat'=>'dd/mm/yy'),
                )
            );
            ?>
    </div>
    <hr />
    <div class="row">
    <?php $this->renderPartial('_viewdetails',array('model'=>$model->detail())); ?>
    </div>
</fieldset>
<fieldset id="packing_list_package" class="ui-widget section">
        <legend class="ui-widget-header ui-corner-all">Shipment Packages</legend>
	<?php 
		$t = count($model->packages());
        foreach($model->packages() as $g=>$package) {
            echo "<div id='packages_container'>";
            $this->renderPartial('_viewpackingform',array('package'=>$package));
            echo "</div>";
            if($g<$t-1)
                echo "<hr>";
        }
        ?>
</fieldset>
</div>
<?php
$cs = Yii::app()->getClientScript();
$viewPackageURL = CHtml::normalizeUrl(array('packing_list/viewpackage','packing_list_id'=>$model->id));
$NewPackageURL = CHtml::normalizeUrl(array('packing_list/NewPackage','packing_list_id'=>$model->id));
$viewDetailURL = CHtml::normalizeUrl(array('packing_list/viewDetails'));
$s =<<<EOD
$('fieldset#packing_list>div.row:eq(0) select').change(function(){
	$('fieldset#packing_list>div.row:eq(0) a:eq(1)').click();
});
$('fieldset#packing_list>div.row:eq(0) a:eq(1)').click(function(){
	var container = $('#packing_list>div.row:eq(2)');
	var select = $('#packing_list>div.row:eq(0) select');  
	$.subcontent.show({
		url:'$viewDetailURL?id='+$(select).val(),
		container:$(container)
	});
	return false;
})
$('fieldset#packing_list>div.row:eq(0) a:eq(0)').click(function(){
	$('#packing_list>div.row:eq(0)').hide();
	var container = $('#packing_list>div.row:eq(2)'); 
	$(container).subcontent({
		url:$(this).attr('href'),
		afterShow:function(){
			$(container).find('input[type="submit"]').unbind();
			$(container).find('input[type="submit"]').bind({'click':function(){
				var newSubmit = $.extend({},$.simplelogform,{afterSubmit:function(data){
											$('#packing_list>div.row:eq(0)').show();
                                            $(container).subcontent({
                                                url:'$viewDetailURL?id='+data.id
                                            });
                                        }
                                    });
                var addition = [];
				addition["packing_list_details[shipping_id]"] = $(container).find('#packing_list_details_shipping_name').attr('shipping_id');
				addition["packing_list_details[port_id]"] = $(container).find('#packing_list_details_port_name').attr('port_id');
            	newSubmit.Submit($(container).find('form'),addition);
            	return false;
			}});
			$(container).find('input#Cancel').click(function(){
                $('#packing_list>div.row:eq(0)').show();
                $('#packing_list>div.row:eq(2)').subcontent({
                	url:'$viewDetailURL?id=$model->packing_details_id'
                });
            });
		}
	});
	return false;
});
$('div#packing_list_update>a:eq(0)').click(function(){
    $('fieldset#packing_list_package').prepend('<div id="packages_container"></div>');
    var container = $('fieldset#packing_list_package>#packages_container:first');
    $(container).hide();
    $(container).subcontent({
        url:'$NewPackageURL',
        afterShow:function(){
        	if($('div#packages_container').length>1)
        		$('div#packages_container:first').after('<hr />');
        	$(container).find('#packing_list_packages_marking').focus();
            $(container).find('input[type="submit"]').bind({'click':function(){
                    var newSubmit = $.extend({},$.simplelogform,{afterSubmit:function(data){
                                            $(container).subcontent({
                                                url:'$viewPackageURL?id='+data.id
                                            });
                                        }
                                    });
                    var i=0;
                    var cargo_id = [];
                    $(container).find('#package_do_list').find('input').each(function(){
                        var name = "packing_list_packages_cargos["+i+"][id]";
                        var value = $(this).attr('cargo_id');
                        cargo_id[name] = value;
                        i++;
                    });
                    newSubmit.Submit($(container).find('form'),cargo_id);
                    return false;
                }
            });
            $(container).find('input#packing_list_packages_cancel').click(function(){
                $('fieldset#packing_list_package>#packages_container:first, fieldset#packing_list_package>hr:first').slideUp().remove();
            });
        }
    });
    return false;
});
EOD;
$cs->registerScript('packing_list#update',$s,CClientScript::POS_END);
?>